package cn.kgc.crowdfundingadmin2.controller;

import cn.kgc.crowdfundingadmin2.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin2.common.ResultEntity;
import cn.kgc.crowdfundingadmin2.pojo.Admin;
import cn.kgc.crowdfundingadmin2.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {

    @Resource
    AdminService adminService;
    /*@RequestMapping(value = "/admin/get_by_id")
    @ResponseBody
    public Admin getByID(Integer id, Model model){
    Admin admin=adminService.seleByid(1);
    return admin;
    }

    *//*跳转登录*//*
    @RequestMapping("/admin/to/login/page.html")
    public String toLoginPage() {
        return "admin-login";
    }*/

    /*登录*/
    @RequestMapping("admin/do/login")
    public String doLogin(String loginAcct, String userPswd, HttpSession session, Model model){
        Admin admin = adminService.login(loginAcct, userPswd);
        if (admin!=null){
            //登录成功
            session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
            return "admin-main";
        }else{
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
            //登录失败
        }

    }


    /*局部异常处理*/
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }

    /*退出*/
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }


    /*分页查询数据*/
    @RequestMapping("/admin/query/for/search")
    public String queryForSearch(

            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword,
            Model model){
        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        for (Admin admin : pageInfo.getList()) {
            System.out.println(admin.getUsername());
        }
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        return "admin-page";
    }

    /*删除*/
    @RequestMapping("/admin/batch/remove")
    @ResponseBody
    public ResultEntity<String> batcheMove(@RequestBody List<Integer> list){
        System.out.println("进来了");
    adminService.remove(list);
    return ResultEntity.successWithoutData();/*返回的json字符串{result,success,message,data}*/
    }


    @RequestMapping("/admin/save")
    public String save(Admin admin){
    adminService.saveAdmin(admin);
    return "redirect:/admin/query/for/search";
    }

    /*打开修改页面*/
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(Integer adminId,Integer pageNum,Model model){
        /*根据id查询admin*/
        Admin admin = adminService.seleAdminById(adminId);

        /*保存数据*/
     model.addAttribute(CrowdFundingConstant.ATTR_ADMIN,admin);
     return "admin-edit";
    }

    /*修改*/
    @RequestMapping("/admin.edit")
    public String edit(Integer pageNum,Admin admin){

        /*修改用户*/
         adminService.updateAdmin(admin);
        /*跳转页面,保持当前页*/
    return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

}
